
<!doctype html>
<html>
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
	<title>设计ajax库需要考虑的问题</title>
</head>
<body>


<script type="text/javascript">

//设计一
var tAjax1 = function(config) {
	var url = config.url;
	var complete = config.complete;
	var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
	xhr.open('post', url);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				complete(xhr.responseText);
			}
		}
	}
	xhr.send();
}


//设计二
var tAjax2 = function(config) {
	var doneFn;

	var url = config.url;
	var complete = config.complete;
	var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
	xhr.open('post', url);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				doneFn(xhr.responseText);
				complete(xhr.responseText);
			}
		}
	}

	xhr.send(xhr.responseText);

	return {
		/**
		 * 返回一个done对象
		 */
		done: function(ourfn) {
			doneFn = ourfn;
		}
	};
}


</script>
</body>
</html>